package com.itany.shop.servlet;

import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 */
@WebServlet("*.cart")
public class CartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        switch (path) {
            case "/add.cart":
                addCart(request, response);
                break;
            case "/modify.cart":
                modifyCart(request, response);
                break;
            case "/removeById.cart":
                removeCart(request, response);
                break;
            case "/removeByIds.cart":
                removeCarts(request, response);
                break;
            case "/clean.cart":
                clean(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请检查请求</h1>");
                break;
        }
    }

    public void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.addCart(cart, id);
            response.sendRedirect(request.getContextPath() + "/findAll.product");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg", "请先登录");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("productMsg", e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request, response);
        }
    }

    public void modifyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        String num = request.getParameter("num");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.modifyCart(cart, id, num);
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg", "请先登录");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request, response);
        }
    }

    public void removeCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        try {
            cartService.removeCart(cart, id);
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg", "请先登录");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request, response);
        }
    }

    public void removeCarts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String[] ids = request.getParameterValues("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            for (String id : ids) {
                cartService.removeCart(cart, id);
            }
            response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg", "请先登录");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request, response);
        }

    }

    public void clean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("cart", new Cart());
        response.sendRedirect(request.getContextPath() + "/jsp/cart.jsp");
    }
}
